package com.itheima.HomeWork_day10;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class TraverseMap03 {
    public static void main(String[] args) {

        //创建集合对象
        Map<String, String> map = new HashMap<String, String>();

        //添加元素
        map.put("张无忌", "赵敏");
        map.put("郭靖", "黄蓉");
        map.put("杨过", "小龙女");

        //遍历集合

        //使用集合键遍历
        Set<String> keySet = map.keySet();


        //遍历set集合
        for (String key : keySet) {
            //通过key取得相应的值
            String value = map.get(key);
            System.out.println(key+"--"+value);
        }

        System.out.println("-------------------------");
        //使用forEach 遍历
        map.forEach((a,b)->{
            System.out.println(a+"--"+b);
        });
        System.out.println("===========================");


        //通过迭代器对来获取
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> it = entries.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        System.out.println("=================================");
        //通过键值对进行遍历
        Set<Map.Entry<String, String>> en = map.entrySet();
        for (Map.Entry<String, String> ent : en) {
            String key = ent.getKey();
            String value = ent.getValue();
            System.out.println(key+"--"+value);
        }

    }
}
